package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
                                                                                                                                                                        import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.OrdersItem;
import com.ruoyi.system.domain.Store;
import com.ruoyi.system.mapper.OrdersItemMapper;
import com.ruoyi.system.mapper.StoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OrdersMapper;
import com.ruoyi.system.domain.Orders;
import com.ruoyi.system.service.IOrdersService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单表Service业务层处理
 *
 * @author zhaohs
 * @date 2021-01-04
 */
@Service
public class OrdersServiceImpl implements IOrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrdersItemMapper ordersItemMapper;

    @Autowired
    private StoreMapper storeMapper;

    /**
     * 查询订单表
     *
     * @param id 订单表ID
     * @return 订单表
     */
    @Override
    public Orders selectOrdersById(Long id) {
        return ordersMapper.selectOrdersById(id);
    }

    /**
     * 查询订单表列表
     *
     * @param orders 订单表
     * @return 订单表
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders) {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增订单表
     *
     * @param orders 订单表
     * @return 结果
     */
            @Override
    public int insertOrders(Orders orders) {
                                                                                                                                                                                                                                                                                    orders.setCreateTime(DateUtils.getNowDate());
                                                                                                    return ordersMapper.insertOrders(orders);
            }

    @Override
    @Transactional
    public String insertOrders(Long userId, Long storeId, List<OrdersItem> items, BigDecimal amount) {
        //
        Orders orders = new Orders();
        orders.setPayStatus(0);
        orders.setOrderStatus(0);
        orders.setPayType(-1);
        orders.setAmount(amount);
        orders.setUserId(userId);

        Store store =  storeMapper.selectStoreById(storeId);
        orders.setReceiverAddress(store.getStoreAddress());
        orders.setReceiverName(store.getStoreName());
        orders.setReceiverTel(store.getStoreTel());

        String orderNo = items.get(0).getOrderNo();
        orders.setOrderNo(orderNo);

        for(OrdersItem i : items){
            ordersItemMapper.insertOrdersItem(i);
        }
        ordersMapper.insertOrders(orders);
        return orderNo;
    }

    /**
     * 修改订单表
     *
     * @param orders 订单表
     * @return 结果
     */
            @Override
    public int updateOrders(Orders orders) {
                                                                                                                                                                                                                                                                                                                                                        return ordersMapper.updateOrders(orders);
    }

    @Override
    public int updateOrdersPayType(String orderNo, Integer payType) {
        Orders orders = new Orders();
        orders.setOrderNo(orderNo);
        Orders o = ordersMapper.selectOrdersList(orders).get(0);
        o.setPayType(payType);
        return ordersMapper.updateOrders(o);
    }

    @Override
    public int updateOrdersPayStatus(String orderNo, Integer payStatus) {
        Orders orders = new Orders();
        orders.setOrderNo(orderNo);
        Orders o = ordersMapper.selectOrdersList(orders).get(0);
        o.setPayStatus(payStatus);
        return ordersMapper.updateOrders(o);
    }

    @Override
    public int updateOrdersCallback(String orderNo) {
        Orders orders = new Orders();
        orders.setOrderNo(orderNo);
        Orders o = ordersMapper.selectOrdersList(orders).get(0);
        o.setPayStatus(1);
        o.setOrderStatus(1);
        o.setPayTime(new Date());
        return ordersMapper.updateOrders(o);
    }

    @Override
    public int confirmReceipt(String orderNo) {
        Orders orders = new Orders();
        orders.setOrderNo(orderNo);
        Orders o = ordersMapper.selectOrdersList(orders).get(0);
        o.setOrderStatus(3);
        o.setCompleteTime(new Date());
        return ordersMapper.updateOrders(o);
    }

    /**
     * 删除订单表对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
            @Override
    public int deleteOrdersByIds(String ids) {
                return ordersMapper.deleteOrdersByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除订单表信息
     *
     * @param id 订单表ID
     * @return 结果
     */
    @Override
    public int deleteOrdersById(Long id) {
                return ordersMapper.deleteOrdersById(id);
    }
        }
